<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:
// +----------------------------------------------------------------------

namespace Common\Api;
class SendSMSApi {

    /**
     * 发送短信
     * @param $mobile_number
     * @param $content
     * @return boolv
     */

    public function sendqSMS($mobile_number,$content){
        $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
        $post_data = "account=cf_gdcj&password=gdcj87985049&mobile=".$mobile_number."&content=".rawurlencode($content);
        //密码可以使用明文密码或使用32位MD5加密
        //$gets =  xml_to_array(Post($post_data, $target));
        $this->Post($post_data, $target);
        return true;
    }

    private function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }

    /**
     * 验证访问IP的有效性
     * @param 访问页面 $module
     * @param 时间间隔 $time_span
     * @param 数据ID $id
     */
    public  function check_ipop_limit($module,$time_span=0,$id=0){
        $op=session($module."_".$id."_ip");
        if(empty($op)) {
            $check['ip']	=	 get_client_ip();
            $check['time']	=	time();
            session($module."_".$id."_ip",$check);
            return true;  //不存在session时验证通过
        } else {
            $check['ip']	=	 get_client_ip();
            $check['time']	=	time();
            if($check['ip']==$op['ip']) {
                if($check['time'] - $op['time'] < $time_span) {
                    return false;
                } else {
                    session($module."_".$id."_ip",$check);
                    return true;  //不存在session时验证通过
                }
            }else {
                session($module."_".$id."_ip",$check);
                return true;  //不存在session时验证通过
            }
        }
    }

    /**
     * 获取随机数
     * @param $len  int  长度
     */
 private function GetRandStr($len){
        $chars = array(
            "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        );
        $charsLen = count($chars) - 1;
        shuffle($chars);
        $output = "";
        for ($i=0; $i<$len; $i++) {
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }

    /**
     * 发送手机验证码
     *@author 作者
     */
    public function sendCode($phone){
        if(!preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#',$phone)){
            $result['msg']= "手机号码不正确";
            $result['code'] = -1;
            $result['status'] = false;
            echo json_encode($result);
            exit;
        }
        if($this->check_ipop_limit("getRegisterCode",30)) {
            $code = $this->GetRandStr(4);
            //保存验证码
            $code_data['create_time'] = time();
            $code_data['mobile'] = $phone;
            $code_data['code'] = $code;
            M('follow_code')->data($code_data)->add();

            session("bind_verify",md5($code.$phone));
            $this->sendqSMS($phone,'尊敬的用户，您绑定手机操作的验证码为'.$code.'，请不要把验证码泄露给其他人。');
            $result['msg']= "可用";
            $result['code'] = 1;
            $result['status'] = true;
        }else{
            $result['msg']= "短信已经在路上，请稍后重试";
            $result['code'] = -1;
            $result['status'] = false;
        }
        header("Content-Type:text/html; charset=utf-8");
        echo json_encode($result);
    }

    /**
     * 检测用户输入的手机短信验证码是否正确
     * @param $phone 手机号码
     * @param $code 用户输入的短信验证码
     * return string
     */
    public function checkCode($phone,$code){
        $session_code=session("bind_verify");
        $new_code=md5($code.$phone);
        if($session_code!=$new_code){
            return false;
        }else{
            session("bind_verify",null);
            return true;
        }
    }
}
